package cn.ffcs.bpmpvc.web;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;

import cn.ffcs.base.web.BaseAction;
import cn.ffcs.bpm.model.TransactProcessStatus;
import cn.ffcs.bpmpvc.services.AppBusinessReqService;
import cn.ffcs.bpmpvc.svc.ProcessPvcService;
import cn.ffcs.bpmpvc.svc.TaskService;
import cn.ffcs.businessrequest.model.BusinessRequest;
import cn.ffcs.businessrequest.model.ProdAdvReq;
import cn.ffcs.businessrequest.model.ReqDocu;
import cn.ffcs.businessrequest.service.BusinessRequestService;
import cn.ffcs.businessrequest.service.ProdAdvReqService;
import cn.ffcs.businessrequest.service.ReqDocuService;
import cn.ffcs.organ.model.Organization;
import cn.ffcs.organ.model.Person;
import cn.ffcs.system.dict.model.Dict;
import cn.ffcs.util.context.ApplicationContextUtil;
import cn.ffcs.util.converter.DateUtil;

/**
 * 
 * TODO 需求单分解与合并action <br/> <table>
 * <tr>
 * <td>负责人/principal: </td>
 * <td colspan="2">liuzhh</td>
 * <tr>
 * <td>修改记录/revision:</td>
 * <td colspan="2"></td>
 * y </tr>
 * <tr>
 * <td>日期:</td>
 * <td>修改人:</td>
 * <td>修改说明:</td>
 * </tr>
 * <tr>
 * <td></td>
 * <td></td>
 * <td></td>
 * </tr>
 * </table>
 * 
 * @author yedw
 * @version $Id$
 */
public class DoDemandAction<D extends TransactProcessStatus> extends
		BaseAction<D> {
	private ProcessPvcService processPvcService;
	private TaskService taskService;
	private AppBusinessReqService appBusinessReqService;
	private BusinessRequestService<BusinessRequest> businessRequestService;
	private ProdAdvReqService<ProdAdvReq> prodAdvReqService;
	private ReqDocuService<ReqDocu> reqDocuService;
	
	private BusinessRequest businessRequest;
	/** 需求类型 * */
	private List<Dict> types;
	/** 重要性 * */
	private List<Dict> importances;
	
	/** 附件上传 * */
	private List<File> uploads;
	private List<String> filetype;
	/** 要删除的文件* */
	private List<String> delDocumentIds;
	private String flowTaskId;
	private String processId;
	
	/**
	 * 查找所有可分解，合并或启动的需求单
	 * 
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String showDemand() {
		HttpServletRequest request = ApplicationContextUtil.getHttpServletRequest();
        Criterion cond = Restrictions.eq("tranStatus", "PASS");
        List tasklist=prodAdvReqService.findBy(cond);
        request.setAttribute("tasklist",tasklist);
		return "showDemand";		
	}
	/**
	 * 查看详细信息
	 * @return
	 */
	public String toInfo() {
		HttpServletRequest request = ApplicationContextUtil.getHttpServletRequest();
		String prodAdvReqId=request.getParameter("prodAdvReqId");
		ProdAdvReq prodAdvReq=getProdAdvReqService().findById(Integer.valueOf(prodAdvReqId));
		Criterion cond1 = Restrictions.eq("refEntity",new ProdAdvReq().getClass().getName());
        Criterion cond2 = Restrictions.eq("refEntityId", Integer.valueOf(prodAdvReqId)+"");
        Criterion condAttach = Restrictions.and(cond1, cond2);
        List<ReqDocu> reqDocus=reqDocuService.findBy(condAttach);
        request.setAttribute("prodAdvReq", prodAdvReq);
		request.setAttribute("reqDocus",  reqDocus);
		return "info";
	}
	/**
	 * 跳转到分解需求单页面
	 * @return
	 */
	public String toResolve() {
		initDict();
		HttpServletRequest request = ApplicationContextUtil.getHttpServletRequest();
		String id=request.getParameter("businessId");
		ProdAdvReq prodAdvReq=getProdAdvReqService().findById(Integer.valueOf(id));
		request.setAttribute("businessRequest", prodAdvReq);
		//得到产品经理角色用户
        List<Person> prodList=appBusinessReqService.findProdListByRole();
        ApplicationContextUtil.getHttpServletRequest().setAttribute("prodList", prodList);
		return "toResolve";
	}
	/**
	 * 提交分解需求单
	 * @return
	 */
	public String resolve() {
		HttpServletRequest request = ApplicationContextUtil.getHttpServletRequest();
		String businessId=request.getParameter("businessReqId");
		String[] name=request.getParameterValues("businessRequest.name");
		String[] startPersonObj=request.getParameterValues("startPersonObj");//发起人
		String[] prodPerson=request.getParameterValues("prodPerson");//产品经理
		String[] typeId=request.getParameterValues("typeId");//需求类型
		String[] importanceId=request.getParameterValues("importanceId");//重要性
		String[] content=request.getParameterValues("content");//内容
		List<BusinessRequest> bsReqList=new ArrayList<BusinessRequest>();
		for (int i = 0; i < name.length; i++) {
			
			BusinessRequest bRequest=new BusinessRequest();
			Person person=getAppBusinessReqService().getAppBusinessReqDao().getPerson(Integer.valueOf(startPersonObj[i]));
			Person person2=getAppBusinessReqService().getAppBusinessReqDao().getPerson(Integer.valueOf(prodPerson[i]));
			Organization organization=getAppBusinessReqService().getOrgByPerson(person);
			Organization organization2=getAppBusinessReqService().getOrgByPerson(person2);
			
			bRequest.setName(name[i]);
			bRequest.setContent(content[i]);
			bRequest.setStartPerson(person.getName());
			bRequest.setStartDept(organization.getName());

			bRequest.setReqCode(getAppBusinessReqService().getReqCode());

			bRequest.setType(typeId[i]);
			bRequest.setImportance(importanceId[i]);			
			
			bRequest.setProdPerson(person2);
			bRequest.setProdDept(organization2);			
			bRequest.setStartPersonObj(person);
			bRequest.setStartDeptObj(organization);
			bRequest.setBegin(new Date());
			bRequest.setProcinstType("02");
			bRequest.setEnd(cn.ffcs.util.converter.DateUtil.getMaxDate());
			bsReqList.add(bRequest);
		}
		getAppBusinessReqService().saveResoleBsReq(businessId, bsReqList,getFlowImproveReqProcessId());
		this.returnMsg="需求单分解操作成功！";
		return "jumpMsg";
	}
	/**
	 * 跳转到合并需求单页面
	 * @return
	 */
	public String toMerge() {
		HttpServletRequest request = ApplicationContextUtil
				.getHttpServletRequest();
		String[] check =request.getParameterValues("chk");
		System.out.println(check.length);
		String allName ="";
		String allContent="";
		String allReqCode="";
		List<ProdAdvReq> reqList=new ArrayList<ProdAdvReq>();
		List<ReqDocu> reqDocus=new ArrayList<ReqDocu>(); 
		for (int i = 0; i < check.length; i++) {
			String prodAdvReqId=check[i];
			ProdAdvReq prodAdvReq=getProdAdvReqService().findById(Integer.valueOf(prodAdvReqId));
			allName=allName+","+prodAdvReq.getName();
			allContent=allContent+","+prodAdvReq.getName();
			allReqCode=allReqCode+","+prodAdvReq.getReqCode();
			reqList.add(prodAdvReq);
			Criterion cond1 = Restrictions.eq("refEntity",new ProdAdvReq().getClass().getName());
	        Criterion cond2 = Restrictions.eq("refEntityId", Integer.valueOf(prodAdvReqId)+"");
	        Criterion condAttach = Restrictions.and(cond1, cond2);
	        List<ReqDocu> tmp=reqDocuService.findBy(condAttach);
	        for (int j = 0; j < tmp.size(); j++) {
	        	reqDocus.add(tmp.get(j));
			}
		}
		if(allName.length()>=1)allName=allName.substring(1);
		if(allContent.length()>=1)allContent=allContent.substring(1);
		if(allReqCode.length()>=1)allReqCode=allReqCode.substring(1);
		request.setAttribute("reqDocus", reqDocus);
		request.setAttribute("reqList", reqList);
		request.setAttribute("allName", allName);
		request.setAttribute("allReqCode", allReqCode);
		request.setAttribute("allContent", allContent);
		initDict();
		List<Person> prodList=appBusinessReqService.findProdListByRole();
        ApplicationContextUtil.getHttpServletRequest().setAttribute("prodList", prodList);
		return "toMerge";
	}
	/**
	 * 提交合并需求单
	 * @return
	 * @throws Exception 
	 */
	public String merge() throws Exception {
		HttpServletRequest request = ApplicationContextUtil.getHttpServletRequest();
		
		BusinessRequest businessRequest=new BusinessRequest();
		businessRequest.setName(request.getParameter("name"));
		businessRequest.setContent(request.getParameter("content"));
		
		String allReqCode=request.getParameter("allReqCode");
		String startPersonObj=request.getParameter("startPersonObj");
		String prodPerson=request.getParameter("prodPerson");
		String importanceId=request.getParameter("importanceId");
		String typeId=request.getParameter("typeId");
		String begin=request.getParameter("prodAdvReq.begin");
		String advanTime=request.getParameter("prodAdvReq.advanTime");
		
		Person person=getAppBusinessReqService().getAppBusinessReqDao().getPerson(Integer.valueOf(startPersonObj));
		Person person2=getAppBusinessReqService().getAppBusinessReqDao().getPerson(Integer.valueOf(prodPerson));
		Organization organization=getAppBusinessReqService().getOrgByPerson(person);
		Organization organization2=getAppBusinessReqService().getOrgByPerson(person2);
		businessRequest.setBegin(new Date());
		businessRequest.setStartPerson(person.getName());
		businessRequest.setStartDept(organization.getName());
		businessRequest.setStartPersonObj(person);
		businessRequest.setStartDeptObj(organization);
		businessRequest.setProdPerson(person2);
		businessRequest.setProdDept(organization2);
		businessRequest.setImportance(importanceId);
		businessRequest.setType(typeId);
		businessRequest.setReqCode(allReqCode);
		businessRequest.setEnd(DateUtil.getMaxDate());
		businessRequest.setProcinstType("02");

		if(advanTime!=null&&!"".equals(advanTime)){
			businessRequest.setAdvanTime(DateUtil.strToDate(advanTime, "yyyy-MM-dd"));
		}
		if(begin!=null&&!"".equals(begin)){
			businessRequest.setBegin(DateUtil.strToDate(begin, "yyyy-MM-dd"));
		}
		//getAppBusinessReqService().saveMergeBsReq(allReqCode, businessRequest,getFlowNewReqProcessId());

		getAppBusinessReqService().saveMergeBsReq(allReqCode, businessRequest,getFlowImproveReqProcessId());

		this.returnMsg="需求单合并操作成功！";
		return "jumpMsg";
	}

	private String getFlowImproveReqProcessId() {
		return getAppBusinessReqService().getFlowImproveReqProcessId();
	}
	private void initDict() {
		String reqNewProdtypes = "REQ_PROD_TYPE";
		String reqImportType = "REQ_IMPORT_TYPE";
		types = getAppBusinessReqService().getAppBusinessReqDao().findDict(reqNewProdtypes);
		importances = getAppBusinessReqService().getAppBusinessReqDao().findDict(reqImportType);
	}

	public ProcessPvcService getProcessPvcService() {
		return processPvcService;
	}

	public void setProcessPvcService(ProcessPvcService processPvcService) {
		this.processPvcService = processPvcService;
	}

	public TaskService getTaskService() {
		return taskService;
	}

	public void setTaskService(TaskService taskService) {
		this.taskService = taskService;
	}

	public BusinessRequest getBusinessRequest() {
		return businessRequest;
	}

	public void setBusinessRequest(BusinessRequest businessRequest) {
		this.businessRequest = businessRequest;
	}

	public AppBusinessReqService getAppBusinessReqService() {
		return appBusinessReqService;
	}

	public void setAppBusinessReqService(
			AppBusinessReqService appBusinessReqService) {
		this.appBusinessReqService = appBusinessReqService;
	}

	public List<Dict> getTypes() {
		return types;
	}

	public void setTypes(List<Dict> types) {
		this.types = types;
	}

	public List<Dict> getImportances() {
		return importances;
	}

	public void setImportances(List<Dict> importances) {
		this.importances = importances;
	}

	public List<String> getFiletype() {
		return filetype;
	}

	public void setFiletype(List<String> filetype) {
		this.filetype = filetype;
	}

	public List<File> getUploads() {
		return uploads;
	}

	public void setUploads(List<File> uploads) {
		this.uploads = uploads;
	}

	public String getProcessId() {
		return processId;
	}

	public void setProcessId(String processId) {
		this.processId = processId;
	}

	public String getFlowTaskId() {
		return flowTaskId;
	}

	public void setFlowTaskId(String flowTaskId) {
		this.flowTaskId = flowTaskId;
	}

	public List<String> getDelDocumentIds() {
		return delDocumentIds;
	}

	public void setDelDocumentIds(List<String> delDocumentIds) {
		this.delDocumentIds = delDocumentIds;
	}

	public BusinessRequestService<BusinessRequest> getBusinessRequestService() {
		return businessRequestService;
	}

	public void setBusinessRequestService(
			BusinessRequestService<BusinessRequest> businessRequestService) {
		this.businessRequestService = businessRequestService;
	}


	public ProdAdvReqService<ProdAdvReq> getProdAdvReqService() {
		return prodAdvReqService;
	}


	public void setProdAdvReqService(ProdAdvReqService<ProdAdvReq> prodAdvReqService) {
		this.prodAdvReqService = prodAdvReqService;
	}


	public ReqDocuService<ReqDocu> getReqDocuService() {
		return reqDocuService;
	}


	public void setReqDocuService(ReqDocuService<ReqDocu> reqDocuService) {
		this.reqDocuService = reqDocuService;
	}

	
	

}
